package com.jyhs.maven.controller;

import com.jyhs.maven.entity.Menu;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping("/menu")
public class MenuController {

//    @RequestMapping(value = {"/t03", "/t03index"})
    @GetMapping("/t03")
    public ModelAndView index() {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("t03");
        modelAndView.addObject("name", "张三");
        return modelAndView;
    }

    @RequestMapping(value = "/t03/add", method = RequestMethod.POST)
    @PostMapping("/t03/add")
    public String add(@RequestParam(value = "name2",required = true) String name,
                      @RequestParam(value = "age",defaultValue = "18") String age) {
        System.out.println("name:" + name);
        System.out.println("age:" + age);
        System.out.println("add");
        return "add";
    }

    @PostMapping("/t03/add2")
    public String add2(Menu menu, Model model) {
        model.addAttribute("name", "李四");
        System.out.println("menu:" + menu);
        System.out.println("add");
        return "add";
    }

    @GetMapping("/t03/add3")
    public String add3( @RequestParam(value = "title2",required = true) String title,String icon,String href,Integer pid) {
        System.out.println("title:" + title);
        System.out.println("add");
        throw new RuntimeException("测试异常");

//        return "add";
    }
//
//
//    @GetMapping ("selectList")
//    @ResponseBody
//    public String selectList() {
//        System.out.println("selectList");
//        return "selectList";
//    }


//    @ExceptionHandler(RuntimeException.class)
//    public ModelAndView handleException(Exception e) {
//        ModelAndView modelAndView = new ModelAndView();
//        modelAndView.setViewName("error");
//        modelAndView.addObject("error", e.getMessage());
//        System.out.println("异常：" + e.getMessage());
//        return modelAndView;
//    }
//
//    @ExceptionHandler(MissingServletRequestParameterException.class)
//    public String handleMissingServletRequestParameterException(Exception e, Model model) {
//        model.addAttribute("error", e.getMessage());
//        System.out.println("参数异常：" + e.getMessage());
//        return "error";
//    }
//    @ExceptionHandler(HttpRequestMethodNotSupportedException.class)
//    public String handleHttpRequestMethodNotSupportedException(Exception e, Model model) {
//        model.addAttribute("error", e.getMessage());
//        System.out.println("请求方式参数异常：" + e.getMessage());
//        return "error";
//    }
//
//    @ExceptionHandler(Exception.class)
//    public String handleException(Exception e, Model model) {
//        model.addAttribute("error", e.getMessage());
//        System.out.println("异常：" + e.getMessage());
//        return "error";
//    }

}
